﻿using System;
using System.Security.Cryptography;

namespace Thor.Models
{
    public class Credentials
    {
        public Guid Id { get; set; }
        public string UserName  { get; set; }
        public string Password { get; set; }
        public string Exponent { get; set; }
        public string Modulus { get; set; }
        public bool Persist { get; set; }
        public string ReferringUrl { get; set; }

        public void Decrypt()
        {
            RSAEncryption rsa = new RSAEncryption(Id.ToString());

            this.Password = rsa.Decrypt(RSAEncryption.AsciiToByteArray(this.Password));
            this.UserName = rsa.Decrypt(RSAEncryption.AsciiToByteArray(this.UserName));
        }

        public static Credentials GetNew()
        {
            Credentials credentials = new Credentials();
            
            credentials.Id = Guid.NewGuid();
            
            RSAEncryption rsa = new RSAEncryption(credentials.Id.ToString());
            
            byte[] exp = rsa.GetPublicKey(credentials.Id.ToString()).Exponent;
            byte[] mod = rsa.GetPublicKey(credentials.Id.ToString()).Modulus;
            
            credentials.Exponent = RSAEncryption.HexToAsciiArray(exp);
            credentials.Modulus = RSAEncryption.HexToAsciiArray(mod);

            return credentials;
        }
    }
}

